package net.cellkyborg.server;

import java.io.*;

class Player {

	int playerID;
	private java.net.Socket mySock;
	private InputStream in;
	private OutputStream out;
	public static final int bufferSize = 1000000;
	public stream inStream = null, outStream = null;
	public Cryption inStreamDecryption = null, outStreamDecryption = null;
	public byte buffer[] = null;

	
	public int playerDesigned = 0;
	public boolean savefile = false, disconnected = true, loaded = false;
	vars playerVars = null;
	int returnCode = 40;

	int ping = 0;

	long lastTimePacketReceived;

	public int lowMemoryVersion;
	public String playerName, playerPass;

	private ButtonHandler buttonHandler = new ButtonHandler(this);

	private void directFlushOutStream() throws IOException {
		out.write(outStream.buffer, 0, outStream.writeOffset);
		outStream.writeOffset = 0;
	}

	private void fillInStream(int forceRead) throws IOException {
		int rd = in.read(inStream.buffer, inStream.writeOffset, forceRead);
		inStream.writeOffset += forceRead;
	}

	Player(java.net.Socket s, int id) {
		playerID = id;
		mySock = s;

		try {
			in = s.getInputStream();
			out = s.getOutputStream();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

		outStream = new stream(new byte[bufferSize]);
		outStream.writeOffset = 0;
		outStream.readOffset = 0;
		inStream = new stream(new byte[bufferSize]);
		inStream.writeOffset = 0;
		inStream.readOffset = 0;

		buffer = new byte[bufferSize];

		long serverSessionKey = 0, clientSessionKey = 0;
		serverSessionKey = ((long) (java.lang.Math.random() * 99999999D) << 32)
				+ (long) (java.lang.Math.random() * 99999999D);

		try {
			fillInStream(1);
			int namePart = inStream.readUnsignedByte();

			outStream.writeByte(0);
			outStream.writeQWord(serverSessionKey);
			directFlushOutStream();

			fillInStream(2);
			int loginType = inStream.readUnsignedByte();
			if (loginType != 16 && loginType != 18) {
				misc.println("Unexpected login type " + loginType);
				return;
			}
			int loginPacketSize = inStream.readUnsignedByte();
			System.out.println("Login packet size: " + loginPacketSize);
			if (loginPacketSize - 93 <= 0) {
				misc.println("Zero RSA packet size!");
				return;
			}
			fillInStream(loginPacketSize);

			if (inStream.readDWord() != 474) {
				misc.println("Not supported version");
				return;
			}

			lowMemoryVersion = inStream.readUnsignedByte();

			for (int i = 0; i < 24; i++)
				System.out.print(inStream.readUnsignedByte() + " ");
			System.out.println();

			for (int i = 0; i < 16; i++) {
				String junk = Integer.toHexString(inStream.readDWord());
			}
			int tmp = inStream.readUnsignedByte();
			if (tmp != 10) {// 10
				misc.println("Encrypted packet Id was " + tmp
						+ " but expected 10");
				return;
			}

			clientSessionKey = inStream.readQWord();
			serverSessionKey = inStream.readQWord();
			playerName = misc.longToPlayerName(inStream.readQWord());
			playerName.toLowerCase();

			if (playerName == null || playerName.length() == 0)
				return;

			playerPass = inStream.readString();
			
			if (playerPass.length() != loginPacketSize - 26 - 93)
				System.out.println("Login packet mismatch: "
						+ playerPass.length() + " "
						+ (loginPacketSize - 26 - 93));
			int sessionKey[] = new int[4];
			sessionKey[0] = (int) (clientSessionKey >> 32);
			sessionKey[1] = (int) clientSessionKey;
			sessionKey[2] = (int) (serverSessionKey >> 32);
			sessionKey[3] = (int) serverSessionKey;

			inStreamDecryption = new Cryption(sessionKey);
			for (int i = 0; i < 4; i++)
				sessionKey[i] += 50;

			outStreamDecryption = new Cryption(sessionKey);
			outStream.packetEncryption = outStreamDecryption;

			returnCode = 2;

			int tloaded = server.sqlHandler.readCharacter(this);
			if (tloaded == 3) {
				return;
			}
			server.sqlHandler.loadMoreInfo(this);

			playerVars = new vars();
			server.sqlHandler.loadVarInt(playerVars, playerName);
			server.sqlHandler.loadVarStr(playerVars, playerName);

			disconnected = false;
			savefile = true;
			absX = 3076;
			absY = 3125;
			mapRegionX = absX / 8;
			mapRegionY = absY / 8;
			currentX = absX - (mapRegionX - 6) * 8;
			currentY = absY - (mapRegionY - 6) * 8;
			System.out.println("X: " + absX + " Y: " + absY);
			System.out.println("mx: " + mapRegionX + " my: " + mapRegionY
					+ " cx: " + currentX + " cy: " + currentY);
			outStream.writeOffset = 0;
			for (int i = 0; i < 50; i++)
				outStream.writeByte(server.updateHandler.LOGIN[i]);
			
			outStream.createFrame(ServerPackets.LOGIN_FRAME15);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.createFrame(ServerPackets.LOGIN_FRAME236);
			outStream.writeByte(78);// 60
			outStream.writeByte(60);// 78
			outStream.writeByte(105);// 170
			outStream.writeByte(170);// 105

			outStream.createFrame(ServerPackets.LOGIN_FRAME251);
			outStream.writeByte(36);// 36
			outStream.writeByte(2);// 2

			outStream.createFrame(ServerPackets.SET_INTERFACE);
			outStream.writeByte(127);
			outStream.writeByte(137);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(90);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME156);

			outStream.createFrame(ServerPackets.SET_INTERFACE);
			outStream.writeByte(127);
			outStream.writeByte(64);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(100);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(48);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// quest
			outStream.writeByte(127);
			outStream.writeByte(18);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(101);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(49);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// inventory
			outStream.writeByte(127);
			outStream.writeByte(149);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(102);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(50);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// equipment
			outStream.writeByte(127);
			outStream.writeByte(131);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(103);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(51);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// prayer
			outStream.writeByte(127);
			outStream.writeByte(15);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(104);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(52);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// magic
			outStream.writeByte(127);
			outStream.writeByte(192);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(105);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(53);//
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// friendlist
			outStream.writeByte(127);
			outStream.writeByte(38);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(107);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(31);
			outStream.writeByte(255);
			outStream.writeByte(127);

			sendStringFrame("Friend List - Titanascape", 38, 2, 2, 0);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// ignorelist
			outStream.writeByte(127);
			outStream.writeByte(39);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(108);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(32);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// logout
			outStream.writeByte(127);
			outStream.writeByte(182);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(109);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(33);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// options
			outStream.writeByte(127);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(110);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(34);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 1
			outStream.writeByte(128);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(57);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 2
			outStream.writeByte(129);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(58);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 3
			outStream.writeByte(128);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(53);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 4
			outStream.writeByte(129);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(54);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 5
			outStream.writeByte(129);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(55);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 6
			outStream.writeByte(128);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(56);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 7
			outStream.writeByte(129);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(59);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 8
			outStream.writeByte(128);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(60);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 9
			outStream.writeByte(129);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(51);

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);// 10
			outStream.writeByte(128);
			outStream.writeByte(1);
			outStream.writeByte(5);
			outStream.writeByte(0);
			outStream.writeByte(52);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// emotions
			outStream.writeByte(127);
			outStream.writeByte(208);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(111);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(35);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// music player
			outStream.writeByte(127);
			outStream.writeByte(239);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(112);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(36);
			outStream.writeByte(255);
			outStream.writeByte(127);

			sendStringFrame("AUTO", 239, 0, 176, 0);

			outStream.createFrame(ServerPackets.SET_INTERFACE);// clan chat
			outStream.writeByte(127);
			outStream.writeByte(77);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(106);
			outStream.writeByte(2);
			outStream.writeByte(36);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(30);
			outStream.writeByte(255);
			outStream.writeByte(127);

			sendSubMenuFrame("Follow", 128, 3);
			sendSubMenuFrame("Trade With", 128, 4);

			sendStringFrame("AUTO", 239, 0, 176, 0);// siaip tai nereikalingas

			outStream.createFrame(ServerPackets.LOGIN_FRAME184);
			outStream.writeByte(129);// 129
			outStream.writeByte(1);
			outStream.writeByte(115);
			outStream.writeByte(0);
			outStream.writeByte(24);// 24

			/*
			outStream.createFrame(ServerPackets.SET_INTERFACE);
			outStream.writeByte(127);
			outStream.writeByte(115);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(77);
			outStream.writeByte(2);
			outStream.writeByte(36);
			 */
			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(47);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.CLOSE_INTERFACE);// removes
			// sidebar
			// interface
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(103);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(51);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.CLOSE_INTERFACE);
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(104);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(52);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.CLOSE_INTERFACE);
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(105);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(53);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.CLOSE_INTERFACE);
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(107);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(31);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.CLOSE_INTERFACE);
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(108);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(32);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.CLOSE_INTERFACE);
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(106);

			outStream.createFrame(ServerPackets.LOGIN_FRAME133);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(127);
			outStream.writeByte(255);// sort of two parts??
			outStream.writeByte(2);
			outStream.writeByte(36);
			outStream.writeByte(0);
			outStream.writeByte(30);
			outStream.writeByte(255);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.LOGIN_FRAME48);
			outStream.writeByte(2);
			outStream.writeByte(12);
			outStream.writeByte(16);
			outStream.writeByte(12);
			outStream.writeByte(47);
			outStream.writeByte(5);

			outStream.createFrame(ServerPackets.SET_MUSIC);
			outStream.writeWordBigEndianA(62);// 190

			sendStringFrame("Newbie Melody", 239, 0, 176, 0);

			addPingPacket();

			outStream.createFrame(ServerPackets.LOGIN_FRAME100);

			outStream.createFrame(ServerPackets.SET_CONFIG);
			outStream.writeByte(0);
			outStream.writeByte(18);
			outStream.writeByte(129);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(22);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 1
			outStream.writeByte(0);
			outStream.writeByte(118);
			outStream.writeByte(127);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 2
			outStream.writeByte(0);
			outStream.writeByte(166);
			outStream.writeByte(130);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 3
			outStream.writeByte(0);
			outStream.writeByte(167);
			outStream.writeByte(128);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 4
			outStream.writeByte(0);
			outStream.writeByte(168);
			outStream.writeByte(132);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 5
			outStream.writeByte(0);
			outStream.writeByte(169);
			outStream.writeByte(128);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 6
			outStream.writeByte(0);
			outStream.writeByte(170);
			outStream.writeByte(128);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 7
			outStream.writeByte(0);
			outStream.writeByte(171);
			outStream.writeByte(128);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 8
			outStream.writeByte(0);
			outStream.writeByte(173);
			outStream.writeByte(128);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 9
			outStream.writeByte(1);
			outStream.writeByte(31);
			outStream.writeByte(128);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(232);
			outStream.writeByte(3);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(1);
			outStream.writeByte(44);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 1
			outStream.writeByte(1);
			outStream.writeByte(48);
			outStream.writeByte(128);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 2- tutorial
			// island finished
			outStream.writeByte(1);
			outStream.writeByte(150);
			outStream.writeByte(135);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(64);
			outStream.writeByte(1);
			outStream.writeByte(230);

			outStream.createFrame(ServerPackets.SET_CONFIG);// 1
			outStream.writeByte(2);
			outStream.writeByte(8);
			outStream.writeByte(129);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(128);
			outStream.writeByte(2);
			outStream.writeByte(41);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(64);
			outStream.writeByte(2);
			outStream.writeByte(111);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(128);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(3);
			outStream.writeByte(20);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(2);
			outStream.writeByte(3);
			outStream.writeByte(42);

			setConfig(849, -1);
			setConfig(850, -1);
			setConfig(851, -1);
			setConfig(852, -1);
			setConfig(853, -1);
			setConfig(854, -1);
			setConfig(855, -1);
			setConfig(856, -1);
			setConfig(872, 0);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(64);
			outStream.writeByte(0);
			outStream.writeByte(3);
			outStream.writeByte(145);

			setConfig(951, -1);
			setConfig(952, -1);
			setConfig(953, -1);
			setConfig(954, -1);
			setConfig(955, -1);
			setConfig(956, -1);
			setConfig(959, -1);
			setConfig(960, -1);
			setConfig(961, -1);
			setConfig(962, -1);
			setConfig(963, -1);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(8);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(3);
			outStream.writeByte(242);

			setConfig(1040, 2);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(4);
			outStream.writeByte(0);
			outStream.writeByte(4);
			outStream.writeByte(20);

			setConfig(1048, 32);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(16);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(4);
			outStream.writeByte(26);

			setConfig(1065, -1);
			setConfig(1066, -1);
			setConfig(1067, -1);
			setConfig(1068, -1);
			setConfig(1069, -1);
			setConfig(1070, -1);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(16);
			outStream.writeByte(4);
			outStream.writeByte(49);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(0);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(4);
			outStream.writeByte(53);

			outStream.createFrame(ServerPackets.LOGIN_FRAME10);
			outStream.writeByte(176);
			outStream.writeByte(64);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(4);
			outStream.writeByte(59);
			outStream.createFrame(ServerPackets.PLAYER_UPDATE);
			outStream.writeSize(62);
			outStream.writeByte(251);
			outStream.writeByte(53);
			outStream.writeByte(152);
			outStream.writeByte(7);
			outStream.writeByte(255);

			outStream.writeByte(24);
			outStream.writeByte(180);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(3);
			outStream.writeByte(132);
			outStream.writeByte(140);
			outStream.writeByte(138);
			outStream.writeByte(98);
			outStream.writeByte(41);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(56);
			outStream.writeByte(3);
			outStream.writeByte(54);
			outStream.writeByte(3);
			outStream.writeByte(53);
			outStream.writeByte(3);
			outStream.writeByte(52);
			outStream.writeByte(3);
			outStream.writeByte(51);
			outStream.writeByte(3);
			outStream.writeByte(55);
			outStream.writeByte(3);
			outStream.writeByte(40);
			outStream.writeByte(3);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(10);
			outStream.writeByte(1);
			outStream.writeByte(42);
			outStream.writeByte(1);
			outStream.writeByte(33);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(1);
			outStream.writeByte(36);
			outStream.writeByte(1);
			outStream.writeByte(26);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(18);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(255);
			outStream.writeByte(255);
			outStream.writeByte(0);
			outStream.writeByte(255);
			outStream.writeByte(127);
			outStream.writeByte(0);

			outStream.createFrame(ServerPackets.NPC_UPDATE);
			outStream.writeSize(1);
			outStream.writeByte(0);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(96);
			outStream.writeByte(160);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(88);
			outStream.writeByte(160);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(80);
			outStream.writeByte(160);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(72);
			outStream.writeByte(160);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(88);
			outStream.writeByte(168);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(80);
			outStream.writeByte(168);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(72);
			outStream.writeByte(168);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(64);
			outStream.writeByte(168);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(96);
			outStream.writeByte(176);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(88);
			outStream.writeByte(176);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(80);
			outStream.writeByte(176);

			outStream.createFrame(ServerPackets.LOGIN_FRAME188);
			outStream.writeByte(42);
			outStream.writeByte(1);
			outStream.writeByte(224);
			outStream.writeByte(45);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(72);
			outStream.writeByte(176);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(64);
			outStream.writeByte(176);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(96);
			outStream.writeByte(184);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(88);
			outStream.writeByte(184);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(80);
			outStream.writeByte(184);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(72);
			outStream.writeByte(184);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(64);
			outStream.writeByte(184);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(96);
			outStream.writeByte(192);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(72);
			outStream.writeByte(192);

			outStream.createFrame(ServerPackets.LOGIN_FRAME169);
			outStream.writeByte(64);
			outStream.writeByte(192);

			outStream.createFrame(ServerPackets.LOGIN_FRAME119);
			outStream.writeSize(41);
			outStream.writeByte(0);
			outStream.writeByte(149);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(93);
			outStream.writeByte(0);
			outStream.writeByte(11);
			outStream.writeByte(255);
			outStream.writeByte(200);
			outStream.writeByte(5);
			outStream.writeByte(255);
			outStream.writeByte(207);
			outStream.writeByte(2);
			outStream.writeByte(255);
			outStream.writeByte(176);
			outStream.writeByte(1);
			outStream.writeByte(255);
			outStream.writeByte(80);
			outStream.writeByte(9);
			outStream.writeByte(255);
			outStream.writeByte(80);
			outStream.writeByte(9);
			outStream.writeByte(255);
			outStream.writeByte(147);
			outStream.writeByte(31);
			outStream.writeByte(255);
			outStream.writeByte(188);
			outStream.writeByte(1);
			outStream.writeByte(255);
			outStream.writeByte(12);
			outStream.writeByte(7);
			outStream.writeByte(255);
			outStream.writeByte(6);
			outStream.writeByte(7);
			outStream.writeByte(0);
			outStream.writeByte(128);
			outStream.writeByte(0);
			outStream.writeByte(255);
			outStream.writeByte(132);
			outStream.writeByte(9);

			outStream.createFrame(ServerPackets.LOGIN_FRAME119);
			outStream.writeSize(8);
			outStream.writeByte(1);
			outStream.writeByte(131);
			outStream.writeByte(0);
			outStream.writeByte(28);
			outStream.writeByte(0);
			outStream.writeByte(94);
			outStream.writeByte(0);
			outStream.writeByte(0);

			for (int i = 0; i < 25; i++) {
				setSkillLevelXP(i, playerLevel[i], playerXP[i]);
			}

			outStream.createFrame(ServerPackets.LOGIN_FRAME66);
			outStream.writeByte(0);
			outStream.writeByte(10);

			outStream.createFrame(ServerPackets.SET_RUN_ENERGY);
			outStream.writeByte(100);

			outStream.createFrame(ServerPackets.LOGIN_FRAME138);

			outStream.createFrame(ServerPackets.LOGIN_FRAME5);
			outStream.writeByte(0);

			for (int i = 0; i < 6; i++) {
				outStream.createFrame(ServerPackets.LOGIN_FRAME4);
				outStream.writeByte(i);
				for (int j = 0; j < 19; j++)
					outStream.writeByte(0);
			}

			directFlushOutStream();
			if (true) {
				lastTimePacketReceived = System.currentTimeMillis();
				savefile = true;
				disconnected = false;
				loaded = true;
				return;
			}
			sendStringFrame("You have not yet set any recovery question", 14,
					0, 122, 1);
			sendStringFrame(
					"You have 0 unread messages in your message centre.", 16,
					0, 122, 1);
			sendStringFrame("You are not a member.", 19, 0, 122, 1);
			sendStringFrame("", 22, 0, 122, 1);
			sendStringFrame("", 21, 0, 122, 1);
			sendStringFrame("", 13, 0, 122, 1);
			sendStringFrame("", 17, 0, 122, 1);// 17,0,122,1
			sendStringFrame("", 1, 0, 21, 0);

			outStream.createFrame(233);
			outStream.writeByte(2);
			outStream.writeByte(37);

			outStream.createFrame(78);// sets interface (login top)
			outStream.writeByte(128);// 128-128
			outStream.writeByte(1);// 1*256
			outStream.writeByte(250);// 250 - 128
			outStream.writeByte(37);// 37
			outStream.writeByte(2);// 2
			outStream.writeByte(3);// 3
			outStream.writeByte(0);// 0

			outStream.createFrame(78);// sets interface (login bottom)
			outStream.writeByte(128);
			outStream.writeByte(0);
			outStream.writeByte(149);// 149
			outStream.writeByte(37);
			outStream.writeByte(2);
			outStream.writeByte(2);
			outStream.writeByte(0);

			outStream.createFrame(134);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(1);
			outStream.writeByte(192);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(87);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(1);
			outStream.writeByte(146);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(88);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(0);
			outStream.writeByte(21);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(89);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(1);
			outStream.writeByte(3);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(90);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(1);
			outStream.writeByte(143);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(91);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(0);
			outStream.writeByte(64);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(92);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(2);
			outStream.writeByte(166);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(94);
			outStream.writeByte(0);

			sendStringFrame("Welcome To Echo, A 474 Server Developed by Evolution X.", 2, 0, 38, 2);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(2);
			outStream.writeByte(167);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(95);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(0);
			outStream.writeByte(54);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(96);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(1);
			outStream.writeByte(133);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(97);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(0);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(57);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(255);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(58);
			outStream.writeByte(0);

			outStream.createFrame(86);// /maybe setconfig
			outStream.writeByte(0);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(53);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(255);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(54);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(255);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(55);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(0);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(56);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(255);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(59);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(0);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(60);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(255);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(51);
			outStream.writeByte(0);

			outStream.createFrame(86);
			outStream.writeByte(0);
			outStream.writeByte(5);
			outStream.writeByte(1);
			outStream.writeByte(52);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(1);
			outStream.writeByte(80);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(98);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(0);
			outStream.writeByte(111);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(99);
			outStream.writeByte(0);

			outStream.createFrame(78);// sets interface
			outStream.writeByte(127);
			outStream.writeByte(0);
			outStream.writeByte(220);
			outStream.writeByte(36);
			outStream.writeByte(2);
			outStream.writeByte(86);
			outStream.writeByte(0);

			sendStringFrame("Unarmed", 0, 0, 92, 0);// 0

			outStream.createFrame(60);// follow??
			outStream.writeByte(9);
			outStream.writeByte(3);
			outStream.writeString("Follow");
			outStream.writeByte(128);

			outStream.createFrame(60);// follow??
			outStream.writeByte(13);
			outStream.writeByte(4);
			outStream.writeString("Trade With");
			outStream.writeByte(128);

			sendStringFrame("AUTO", 176, 0, 239, 0);// 0

			outStream.createFrame(177);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			sendMessageFrame("Welcome To Echo, A 474 Server Developed by Evolution X.");

			outStream.createFrame(23);
			outStream.writeByte(0);
			outStream.writeByte(12);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(2);
			outStream.writeByte(101);
			outStream.writeByte(102);
			outStream.writeByte(0);
			outStream.writeByte(101);
			outStream.writeByte(99);
			outStream.writeByte(0);

			outStream.createFrame(31);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(18);
			outStream.writeByte(255);

			outStream.createFrame(119);
			outStream.writeByte(21);
			outStream.writeByte(0);
			outStream.writeByte(4);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.createFrame(119);
			outStream.writeByte(22);
			outStream.writeByte(0);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(23);
			outStream.writeByte(192);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(43);
			outStream.writeByte(255);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(166);
			outStream.writeByte(254);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(167);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(168);
			outStream.writeByte(252);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(169);
			outStream.writeByte(252);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(170);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(171);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(0);
			outStream.writeByte(173);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(1);
			outStream.writeByte(31);
			outStream.writeByte(0);

			outStream.createFrame(119);
			outStream.writeByte(44);
			outStream.writeByte(1);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(3);
			outStream.writeByte(232);

			outStream.writeByte(151);
			outStream.writeByte(1);
			outStream.writeByte(48);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(1);
			outStream.writeByte(150);
			outStream.writeByte(236);

			outStream.createFrame(119);
			outStream.writeByte(230);
			outStream.writeByte(1);
			outStream.writeByte(64);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(2);
			outStream.writeByte(8);
			outStream.writeByte(255);

			outStream.createFrame(119);
			outStream.writeByte(41);
			outStream.writeByte(2);
			outStream.writeByte(128);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.createFrame(119);
			outStream.writeByte(111);
			outStream.writeByte(2);
			outStream.writeByte(64);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.createFrame(119);
			outStream.writeByte(20);
			outStream.writeByte(3);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(128);

			outStream.createFrame(119);
			outStream.writeByte(42);
			outStream.writeByte(3);
			outStream.writeByte(2);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(81);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(82);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(83);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(84);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(85);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(86);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(87);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(88);
			outStream.writeByte(1);

			outStream.writeByte(151);
			outStream.writeByte(3);
			outStream.writeByte(104);
			outStream.writeByte(252);

			outStream.createFrame(119);
			outStream.writeByte(145);
			outStream.writeByte(3);
			outStream.writeByte(0);
			outStream.writeByte(64);
			outStream.writeByte(0);
			outStream.writeByte(0);

			for (int i = 183; i < 189; i++) {
				outStream.writeByte(151);
				outStream.writeByte(3);
				outStream.writeByte(i);
				outStream.writeByte(1);
			}
			for (int i = 191; i < 196; i++) {
				outStream.writeByte(151);
				outStream.writeByte(3);
				outStream.writeByte(i);
				outStream.writeByte(1);
			}

			outStream.createFrame(119);
			outStream.writeByte(242);
			outStream.writeByte(3);
			outStream.writeByte(0);
			outStream.writeByte(0);
			outStream.writeByte(8);
			outStream.writeByte(0);

			outStream.createFrame(195);
			outStream.writeByte(0);
			outStream.writeByte(120);
			for (int i = 0; i < 120; i++) {
				outStream.writeByte(server.updateHandler.CHAR[i]);
			}

			outStream.createFrame(73);
			outStream.writeByte(0);
			outStream.writeByte(1);
			outStream.writeByte(0);

			outStream.createFrame(101);
			outStream.writeByte(168);
			outStream.writeByte(244);

			outStream.createFrame(101);
			outStream.writeByte(176);
			outStream.writeByte(244);

			outStream.createFrame(25);
			outStream.writeByte(129);
			outStream.writeByte(0);
			outStream.writeByte(132);
			outStream.writeByte(7);
			outStream.writeByte(143);

			outStream.createFrame(101);
			outStream.writeByte(184);
			outStream.writeByte(244);

			outStream.createFrame(25);
			outStream.writeByte(129);
			outStream.writeByte(0);
			outStream.writeByte(129);
			outStream.writeByte(5);
			outStream.writeByte(231);

			outStream.createFrame(25);
			outStream.writeByte(129);
			outStream.writeByte(0);
			outStream.writeByte(144);
			outStream.writeByte(5);
			outStream.writeByte(231);

			outStream.createFrame(101);
			outStream.writeByte(176);
			outStream.writeByte(216);

			outStream.createFrame(101);
			outStream.writeByte(160);
			outStream.writeByte(208);

			outStream.createFrame(101);
			outStream.writeByte(168);
			outStream.writeByte(208);

			outStream.createFrame(25);
			outStream.writeByte(129);
			outStream.writeByte(0);
			outStream.writeByte(215);
			outStream.writeByte(4);
			outStream.writeByte(241);

			outStream.createFrame(101);
			outStream.writeByte(176);
			outStream.writeByte(208);

			outStream.createFrame(25);
			outStream.writeByte(129);
			outStream.writeByte(0);
			outStream.writeByte(215);
			outStream.writeByte(4);
			outStream.writeByte(241);

			outStream.createFrame(101);
			outStream.writeByte(184);
			outStream.writeByte(208);

			outStream.createFrame(101);
			outStream.writeByte(160);
			outStream.writeByte(200);

			outStream.createFrame(101);
			outStream.writeByte(160);
			outStream.writeByte(192);

			outStream.createFrame(101);
			outStream.writeByte(168);
			outStream.writeByte(192);

			outStream.createFrame(101);
			outStream.writeByte(176);
			outStream.writeByte(192);

			outStream.createFrame(200);// some changes in magic bar
			outStream.writeByte(0);
			outStream.writeByte(62);
			for (int i = 0; i < 62; i++) {
				outStream.writeByte(server.updateHandler.F200[i]);
			}

			outStream.createFrame(200);
			outStream.writeByte(0);
			outStream.writeByte(8);
			outStream.writeByte(1);
			outStream.writeByte(131);
			outStream.writeByte(0);
			outStream.writeByte(28);
			outStream.writeByte(0);
			outStream.writeByte(94);
			outStream.writeByte(0);
			outStream.writeByte(0);

			outStream.createFrame(55);// irgi kazkas su interface
			outStream.writeByte(0);
			outStream.writeByte(107);
			for (int i = 0; i < 107; i++) {
				outStream.writeByte(server.updateHandler.SKILLXP[i]);
			}

			for (int i = 0; i < 25; i++) {
				setSkillLevelXP(i, playerLevel[i], playerXP[i]);
			}

			outStream.createFrame(225);
			outStream.writeByte(0);
			outStream.writeByte(16);

			outStream.createFrame(197);
			outStream.writeByte(17);

			outStream.createFrame(110);

			outStream.createFrame(104);
			outStream.writeByte(0);

			directFlushOutStream();

			lastTimePacketReceived = System.currentTimeMillis();
			savefile = true;
		} catch (IOException ex) {
			savefile = false;
			disconnected = true;
			loaded = true;
			ex.printStackTrace();
		}
	}

	public void disconnect() {
		try {
			out.write(returnCode);
			mySock.close();
		} catch (IOException io) {
		}
	}

	public void sendSubMenuFrame(String str, int i, int j) {
		int pSize = str.length() + 3;
		outStream.createFrame(ServerPackets.SET_SUBMENU);
		outStream.writeByte(pSize);
		outStream.writeByte(i);
		outStream.writeString(str);
		outStream.writeByteC(j);
	}

	public void sendStringFrame(String str, int i, int j, int k, int l) {
		int sSize = str.length() + 5;
		outStream.createFrame(ServerPackets.SET_STRING);
		outStream.writeByte(sSize / 256);
		outStream.writeByte(sSize % 256);
		outStream.writeByte(i);
		outStream.writeByte(j);
		outStream.writeByte(k);
		outStream.writeByte(l);
		outStream.writeString(str);
	}

	public void sendMessageFrame(String str) {
		outStream.createFrame(135);
		outStream.writeByte(str.length() + 1);
		outStream.writeString(str);
	}

	public void setSkillLevelXP(int skillID, int skillLevel, int skillXP) {
		outStream.createFrame(ServerPackets.SET_SKILL_LEVEL_XP);
		outStream.writeByte(skillID);
		outStream.writeDWord_v2(skillXP);
		outStream.writeByte(skillLevel);
	}

	public void setConfig(int itemID, int value) {
		outStream.createFrame(ServerPackets.SET_CONFIG);
		outStream.writeWord(itemID);
		outStream.writeByteA(value);
	}

	public void showSidebarInterface(int interfaceID, int sidebar) {
		outStream.createFrame(102);
		outStream.writeByte(interfaceID);
		outStream.writeByte(interfaceID >> 8);
		outStream.writeByteC(sidebar);
	}

	int pingDelay = 0;

	public void process() {
		try {
			pingDelay++;
			if (pingDelay == 10) {
				addPingPacket();
				pingDelay = 0;
			}
			int packetSize = in.available();
			if (packetSize > 0) {
				fillInStream(packetSize);
				while (parseData())
					;

				if (inStream.available() < 1) {
					inStream.writeOffset = 0;
					inStream.readOffset = 0;
				}
				lastTimePacketReceived = System.currentTimeMillis();
			}
			directFlushOutStream();
			if (System.currentTimeMillis() - lastTimePacketReceived > 60000) {
				disconnected = true;
			}
		} catch (IOException ioex) {
			ioex.printStackTrace();
		}
	}

	int[] packetSizes = { -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 0-9
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 10-19
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 20-29
			-3, -3, -3, -3, 1, -3, -3, -3, -3, -3, // 30-39
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 40-49
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 50-59
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 60-69
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 70-79
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 80-89
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 90-99
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 100-109
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 110-119
			-3, -1, -3, -3, -3, -3, -3, -3, -3, -3, // 120-129
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 130-139
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -1, // 140-149
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 150-159
			-3, -3, -3, -3, -3, -3, -3, 4, -3, -3, // 160-169
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 170-179
			-3, -3, -3, -3, -3, -3, 0, -3, -3, -3, // 180-189
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 190-199
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 200-209
			-3, -3, -3, -3, -3, 4, -3, -3, -3, -3, // 210-219
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 220-229
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 230-239
			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 240-249
			-3, -3, -3, -3, -3, -3 // 250-255
	};

	private boolean parseData() {
		if (inStream.available() < 1)
			return false;

		int packetType = inStream.readUnsignedByte();
		int packetSize = packetSizes[packetType];

		if (packetSize >= 0) {
			if (inStream.available() < packetSize) {
				inStream.readOffset -= 1;
				return false;
			} else {
				return handleData(packetType, packetSize);
			}
		} else if (packetSize == -1) {
			if (inStream.available() < 1) {
				inStream.readOffset -= 1;
				return false;
			} else {
				packetSize = inStream.readUnsignedByte();
				if (inStream.available() < packetSize) {
					inStream.readOffset -= 2;
					return false;
				} else {
					return handleData(packetType, packetSize);
				}
			}
		} else if (packetSize == -2) {
			if (inStream.available() < 2) {
				inStream.readOffset -= 1;
				return false;
			} else {
				packetSize = inStream.readSize();
				if (inStream.available() < packetSize) {
					inStream.readOffset -= 3;
					return false;
				} else {
					return handleData(packetType, packetSize);
				}
			}
		} else {
			int avail = inStream.available();
			handleData(packetType, avail);
			return false;
		}
	}

	private boolean handleData(int packetType, int packetSize) {
		switch (packetType) {

		case ClientPackets.WINDOW_STATE:
			int actv = inStream.readUnsignedByte();
			if (actv == 0)
				active = false;
			else if (actv == 1)
				active = true;
			break;
		case ClientPackets.WIELD_ITEM:// wield item
			int junk = inStream.readDWord();// 0 0 149 0
			int slot = inStream.readUnsignedByteA();
			int junk2 = inStream.readUnsignedByte();// 0
			int itemID = inStream.readUnsignedWordA();
			misc.println("Wield item id: " + itemID + " slot: " + slot
					+ " Junk1: " + junk + " junk2: " + junk2);
			break;
		case ClientPackets.CHANGE_SLOT_INV:// change inventory item place
			inStream.readOffset += 6;
			int slot1 = inStream.readUnsignedByteA();
			int slot2 = inStream.readUnsignedByte();
			int junk1 = inStream.readUnsignedByte();
			misc.println("Change item place from " + slot1 + " to " + slot2);
			// showBuffer(packetSize);
			break;
		case ClientPackets.CAMERA_CHANGE: // camera angle change
			inStream.readOffset += packetSize;
			break;

		case ClientPackets.BUTTON_PRESSED: // button pressed
			int interID = inStream.readSize();
			int buttID = inStream.readSize();
			misc.println("Button pressed: Iterface ID - " + interID
					+ " Button ID - " + buttID);
			buttonHandler.buttonPressed(interID, buttID);
			break;

		case ClientPackets.MOUSE_MOVED:
		case ClientPackets.MOUSE_PRESSED:// mouse pressed
			inStream.readOffset += packetSize;
			break;

		case ClientPackets.PING:// ping

			break;
		case ClientPackets.PING_REPLY:
			inStream.readOffset += packetSize;
			break;

		default:
			misc.println("Unhandled packet " + packetType + " size of "
					+ packetSize);
			printBuffer(packetSize);
			// inStream.writeOffset=0;
			// inStream.readOffset=0;
			return false;
		}
		return true;
	}

	private void printBuffer(int len) {
		for (int i = 0; i < len; i++) {
			System.out.print(inStream.readUnsignedByte() + " ");
		}
		misc.println(".");
	}

	void move() {
		stream st = new stream(new byte[1024]);
		st.writeBits(1, 1);
		st.writeBits(2, 1);
		st.writeBits(3, 6);
		st.writeBits(1, 0);
		st.writeBits(8, 0);
		st.writeBits(11, 2047);

		outStream.createFrame(ServerPackets.PLAYER_UPDATE);
		outStream.writeSize(4);
		for (int i = 0; i < 4; i++)
			outStream.writeByte(st.readUnsignedByte());
	}

	void addPingPacket() {
		outStream.createFrame(ServerPackets.PING);
		outStream.writeSize(6);
		outStream.writeByte(1);
		outStream.writeDWord(ping++);
		outStream.writeByte(255);
		/*
		 * outStream.writeByte(2); outStream.writeByte(107);
		 * outStream.writeByte(100); outStream.writeByte(0);
		 * outStream.writeByte(82); outStream.writeByte(0);
		 */
	}

	void addPingPacket2(int[] arr, int type) {
		outStream.createFrame(195);
		outStream.writeSize(arr.length + 3);

		outStream.writeByte(type);
		outStream.writeByte(arr.length + 1);

		for (int i = 0; i < arr.length; i++)
			outStream.writeByte(arr[i]);

		outStream.writeByte(128);

		outStream.createFrame(73);
		outStream.writeSize(1);
		outStream.writeByte(0);
	}

	int heightLevel;
	int teleportToX, teleportToY;
	int absX, absY;
	int mapRegionX, mapRegionY;
	int currentX, currentY;
	int playerRights;
	int playerIsMember;
	int playerMessages;
	String playerLastConnect;
	int playerLastLogin;

	int ancients;
	int totalqp;
	int pHead;
	int pBeard;
	int pTorso;
	int pArms;
	int pHands;
	int pLegs;
	int pFeet;

	int Mime1;
	int Mime2;
	int Mime3;
	int Mime4;

	int specialEnergy;
	int playerEnergy;

	int playerLook[] = new int[6];
	int playerXP[] = new int[25];
	int playerLevel[] = new int[25];
	int playerEquipment[] = new int[14];
	int playerEquipmentN[] = new int[14];
	int playerItems[] = new int[28];
	int playerItemsN[] = new int[28];
	int bankItems[] = new int[800];
	int bankItemsN[] = new int[800];
	long playerFriends[] = new long[100];
	long playerIgnores[] = new long[100];

	private boolean active = true;

	static byte b1 = (byte) (int) 37;
	static byte b2 = (byte) (int) 2;
	static byte b3 = (byte) (int) 3;
	static byte b4 = (byte) (int) 0;

	static long f = 35979267;

	static int[] capes = { 35, 667, 1265, 1267, 1269, 1271, 1273, 1275, 1277,
			1279, 1281, 1283, 1285, 1287, 1289, 1291, 1293, 1295, 1297, 1299,
			1301, 1303, 1305, 1321, 1323, 1325, 1327, 1329, 1331, 1333, 1335,
			1337, 1339, 1341, 1343, 1345, 1347, 1349, 1351, 1353, 1355, 1357,
			1359, 1361, 1363, 1365, 1367, 1369, 1371, 1373, 1375, 1377, 1379,
			1381, 1383, 1385, 1387, 1389, 1391, 1393, 1395, 1397, 1399, 1401,
			1403, 1405, 1407, 1409, 1420, 1422, 1424, 1426, 1428, 1430, 1432,
			1434, 2415, 2416, 2417, 4151 };

	public static void main(String args[]) {
		
		stream outStream = new stream();
		
		outStream.writeByte(0);
		outStream.writeByte(0);
		outStream.writeByte(0);
		outStream.writeByte(2);
		outStream.writeByte(0);
		outStream.writeByte(22);
		
		System.out.println(outStream.readDWordBigEndian());
		System.out.println(outStream.readUnsignedWord());
		if (true) return;
		System.out.println((1 << -647192592));
	}

	int[] anIntArray487 = (new int[] {// serverio paketu dydziai
	0, 0, 0, 0, 0, 0, 0, 0, 0, -2, // 0-9
			0, 1, 0, 6, 0, 4, 4, 0, 0, 0, // 10-19
			0, 0, 0, -2, 0, 5, 0, 0, 0, 0, // 20-29
			-2, 0, 0, 8, 0, 0, 6, 0, 0, 6, // 30-39
			0, 3, 0, -2, 0, 0, 0, 0, 0, 0, // 40-49
			4, 0, 0, 11, 0, -2, 0, 0, 10, 0, // 50-59
			-1, 0, -1, 0, 0, 0, 0, 0, 0, 0, // 60-69
			-2, 10, 0, -2, 0, 0, 4, 0, 7, 0, // 70-79
			0, 0, 0, 0, 0, 0, 5, 0, 0, 0, // 80-89
			6, 0, 0, 0, 0, 2, 0, 0, 0, 0, // 90-99
			15, 2, 3, 0, 1, 0, 0, 0, 0, 0, // 100-109
			0, 0, 0, 0, 0, 5, 0, 0, 0, 6, // 110-119
			2, -2, 0, 0, 0, 0, -2, 0, 3, 6, // 120-129
			0, 0, 2, -1, 0, -1, 0, 0, 0, 0, // 130-139
			0, 7, 0, 0, 5, 0, 0, 6, 0, 6, // 140-149
			0, 3, 0, 0, 0, 8, 0, 0, 12, 0, // 150-159
			0, 0, 0, 0, 7, 0, 0, 0, 0, 4, // 160-169
			4, 0, 0, 6, 0, 0, 0, 6, 0, 0, // 170-179
			0, 0, 0, 0, 0, 0, 6, 0, 0, 0, // 180-189
			0, 0, 0, 0, 0, -2, 0, 1, 0, 0, // 190-199
			-2, 0, 0, 0, 0, 0, 0, 0, 0, 4, // 200-209
			2, 0, 0, 0, 0, 0, 0, 0, -2, -2, // 210-219
			-1, 0, 0, 0, 5, 2, 0, 0, 0, 0, // 220-229
			0, 0, 0, 2, 0, 0, 0, 0, 0, 2, // 230-239
			6, 0, 11, 0, 0, 14, 0, 0, 0, 0, // 240-249
			0, 0, 0, 0, 0, 0 // 250-255
	});

}